clear all
use data/judgeyear.dta

rename electtime2 electtime
gen elect0 = electtime == 0
foreach j in "1" "2" {
gen electafter`j' = electtime == `j'
gen electbefore`j' = electtime == -`j' 
}

gen electwindow = electtime >= -3 & electtime <= 2

gen C = howretain == "P" | howretain == "NP"
gen U = howretain == "U"


foreach y in numops numdisc numwords numwords_maj_m numtoc_maj_m poscites_m distcites_m negcites_m discusscites_m quotecites_m outstatecites_m {
gen log`y' = log(1+`y')

}

preserve
quietly areg lognumops i.year electbefore2 electbefore1 elect0 electafter1 electafter2 electwindow electwindow if C & termlength >= 6, absorb(judgefe) cluster(judgefe) 
parmest, norestore level(90)
gen otime=.
foreach j in "1" "2"   {
replace otime = -`j' if parm== "electbefore`j'"
replace otime = `j' if parm== "electafter`j'"
}
replace otime = 0 if parm == "elect0"
replace otime = -3 if strpos(parm, "o.")
drop if missing(otime)
sort otime
twoway line estimate otime || rline min90 max90 otime, lp(dash) ||, xline(0.5, lcolor(black) lwidth(vthin)) yline(0, lcolor(gray) lwidth(vthin)) xtitle(Years Before and After Re-election Event) legend(off) ylabel(-.15(.05).15) ytitle(Log Number of Majority Opinions)
graph save figures/elect-numops-C.gph, replace
graph export figures/elect-numops-C.png, replace
restore

preserve
quietly areg lognumops i.year electbefore2 electbefore1 elect0 electafter1 electafter2 electwindow electwindow if U & termlength >= 6, absorb(judgefe) cluster(judgefe) 
parmest, norestore level(90)
gen otime=.
foreach j in "1" "2"   {
replace otime = -`j' if parm== "electbefore`j'"
replace otime = `j' if parm== "electafter`j'"
}
replace otime = 0 if parm == "elect0"
replace otime = -3 if strpos(parm, "o.")
drop if missing(otime)
sort otime
twoway line estimate otime || rline min90 max90 otime, lp(dash) ||, xline(0.5, lcolor(black) lwidth(vthin)) yline(0, lcolor(gray) lwidth(vthin)) xtitle(Years Before and After Re-election Event) legend(off) ylabel(-.15(.05).15) ytitle(Log Number of Majority Opinions)
graph save figures/elect-numops-U.gph, replace
graph export figures/elect-numops-U.png, replace
restore

preserve
quietly areg lognumdisc i.year electbefore2 electbefore1 elect0 electafter1 electafter2 electwindow electwindow if C & termlength >= 6, absorb(judgefe) cluster(judgefe) 
parmest, norestore level(90)
gen otime=.
foreach j in "1" "2"   {
replace otime = -`j' if parm== "electbefore`j'"
replace otime = `j' if parm== "electafter`j'"
}
replace otime = 0 if parm == "elect0"
replace otime = -3 if strpos(parm, "o.")
drop if missing(otime)
sort otime
twoway line estimate otime || rline min90 max90 otime, lp(dash) ||, xline(0.5, lcolor(black) lwidth(vthin)) yline(0, lcolor(gray) lwidth(vthin)) xtitle(Years Before and After Re-election Event) legend(off) ylabel(-.15(.05).15) ytitle(Log Number of Discretionary Opinions)
graph save figures/elect-numdisc-C.gph, replace
graph export figures/elect-numdisc-C.png, replace
restore

preserve
quietly areg lognumdisc i.year electbefore2 electbefore1 elect0 electafter1 electafter2 electwindow electwindow if U & termlength >= 6, absorb(judgefe) cluster(judgefe) 
parmest, norestore level(90)
gen otime=.
foreach j in "1" "2"   {
replace otime = -`j' if parm== "electbefore`j'"
replace otime = `j' if parm== "electafter`j'"
}
replace otime = 0 if parm == "elect0"
replace otime = -3 if strpos(parm, "o.")
drop if missing(otime)
sort otime
twoway line estimate otime || rline min90 max90 otime, lp(dash) ||, xline(0.5, lcolor(black) lwidth(vthin)) yline(0, lcolor(gray) lwidth(vthin)) xtitle(Years Before and After Re-election Event) legend(off) ylabel(-.15(.05).15) ytitle(Log Number of Discretionary Opinions)
graph save figures/elect-numdisc-U.gph, replace
graph export figures/elect-numdisc-U.png, replace
restore

preserve
quietly areg lognumtoc_maj_m i.year electbefore2 electbefore1 elect0 electafter1 electafter2 electwindow electwindow if C & termlength >= 6, absorb(judgefe) cluster(judgefe) 
parmest, norestore level(90)
gen otime=.
foreach j in "1" "2"   {
replace otime = -`j' if parm== "electbefore`j'"
replace otime = `j' if parm== "electafter`j'"
}
replace otime = 0 if parm == "elect0"
replace otime = -3 if strpos(parm, "o.")
drop if missing(otime)
sort otime
twoway line estimate otime || rline min90 max90 otime, lp(dash) ||, xline(0.5, lcolor(black) lwidth(vthin)) yline(0, lcolor(gray) lwidth(vthin)) xtitle(Years Before and After Re-election Event) legend(off) ylabel(-.15(.05).15) ytitle(Log Length of Table of Cases)
graph save figures/elect-numtoc-C.gph, replace
graph export figures/elect-numtoc-C.png, replace
restore

preserve
quietly areg lognumtoc_maj_m i.year electbefore2 electbefore1 elect0 electafter1 electafter2 electwindow electwindow if U & termlength >= 6, absorb(judgefe) cluster(judgefe) 
parmest, norestore level(90)
gen otime=.
foreach j in "1" "2"   {
replace otime = -`j' if parm== "electbefore`j'"
replace otime = `j' if parm== "electafter`j'"
}
replace otime = 0 if parm == "elect0"
replace otime = -3 if strpos(parm, "o.")
drop if missing(otime)
sort otime
twoway line estimate otime || rline min90 max90 otime, lp(dash) ||, xline(0.5, lcolor(black) lwidth(vthin)) yline(0, lcolor(gray) lwidth(vthin)) xtitle(Years Before and After Re-election Event) legend(off) ylabel(-.15(.05).15) ytitle(Log Length of Table of Cases)
graph save figures/elect-numtoc-U.gph, replace
graph export figures/elect-numtoc-U.png, replace
restore



preserve
quietly areg logposcites_m i.year electbefore2 electbefore1 elect0 electafter1 electafter2 electwindow electwindow if C & termlength >= 6, absorb(judgefe) cluster(judgefe) 
parmest, norestore level(90)
gen otime=.
foreach j in "1" "2"   {
replace otime = -`j' if parm== "electbefore`j'"
replace otime = `j' if parm== "electafter`j'"
}
replace otime = 0 if parm == "elect0"
replace otime = -3 if strpos(parm, "o.")
drop if missing(otime)
sort otime
twoway line estimate otime || rline min90 max90 otime, lp(dash) ||, xline(0.5, lcolor(black) lwidth(vthin)) yline(0, lcolor(gray) lwidth(vthin)) xtitle(Years Before and After Re-election Event) legend(off) ylabel(-.15(.05).15) ytitle(Log Positive Cites Per Opinion)
graph save figures/elect-poscites-C.gph, replace
graph export figures/elect-poscites-C.png, replace
restore

preserve
quietly areg logposcites_m i.year electbefore2 electbefore1 elect0 electafter1 electafter2 electwindow electwindow if U & termlength >= 6, absorb(judgefe) cluster(judgefe) 
parmest, norestore level(90)
gen otime=.
foreach j in "1" "2"   {
replace otime = -`j' if parm== "electbefore`j'"
replace otime = `j' if parm== "electafter`j'"
}
replace otime = 0 if parm == "elect0"
replace otime = -3 if strpos(parm, "o.")
drop if missing(otime)
sort otime
twoway line estimate otime || rline min90 max90 otime, lp(dash) ||, xline(0.5, lcolor(black) lwidth(vthin)) yline(0, lcolor(gray) lwidth(vthin)) xtitle(Years Before and After Re-election Event) legend(off) ylabel(-.15(.05).15) ytitle(Log Positive Cites Per Opinion)
graph save figures/elect-poscites-U.gph, replace
graph export figures/elect-poscites-U.png, replace
restore